#include "mainwindow.h"
#include "ui_mainwindow.h"



MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    // 初始化滚动条
    this->scrollArea = nullptr;
    //初始化时间
    this->initAlarmClock();
    this->tray = new ZygTrayIcon(this);


    //主窗体
    ui->setupUi(this);
    this->setWindowTitle("张余歌clock");
    this->setFixedSize(240,300);

    //主窗体背景色
    ZygBackground zygBackground(MAIN_BACKGROUND);
    this->setPalette(zygBackground);


    //状态栏
    QMenuBar *bar = menuBar();
    this->setMenuBar(bar);
    QMenu *optionMenu = bar->addMenu("操作");


    // 清空操作
    QAction *clearAction = optionMenu->addAction("清空");
    connect(clearAction,&QAction::triggered,[=](){
       QDialog *dlg = new QDialog(this);
       dlg->setFixedSize(300,240);
       dlg->setWindowTitle("清空闹钟");
       QLabel* label = new QLabel("确定要清空闹钟吗?",dlg);
       label->setFont(ZygFont::getFont(20));

       //添加窗体背景色
       ZygBackground clearBackground(CLEAR_BACKGROUND);
       dlg->setPalette(clearBackground);

       QPushButton *ok = new QPushButton("确定",dlg);
       ok->move((dlg->width() - ok->width() * 2 - 50) / 2 , dlg->height() - ok->height() );
       connect(ok,&QPushButton::clicked,[=](){
           ZygTimer::clearList();
           this->reflashDisplayTimerList();
           dlg->close();
       });

       QPushButton *cancel = new QPushButton("取消",dlg);
       cancel->move(ok->x() + ok->width() + 50, ok->y());
       connect(cancel,&QPushButton::clicked,[=](){
           dlg->close();
       });

       dlg->setAttribute(Qt::WA_DeleteOnClose);
       dlg->show();
    });

    //添加操作
    QAction *addAction = optionMenu->addAction("添加");
    connect(addAction,&QAction::triggered,[=](){
        EditClockDidalog *editClockDialog = new EditClockDidalog(this,nullptr);
        editClockDialog->setAttribute(Qt::WA_DeleteOnClose);
        editClockDialog->show();
        connect(editClockDialog,&EditClockDidalog::closed,[=](){
            this->reflashDisplayTimerList();
        });
    });


    //显示闹钟
    this->reflashDisplayTimerList();
}

MainWindow::~MainWindow()
{
    delete this->tray;
    delete this->scrollArea;
    delete ui;
}


void MainWindow::reflashDisplayTimerList(){
//    ZygTimer::printList();
    if(this->scrollArea == nullptr){
        this->scrollArea = new ZygScrollArea(this);
    }else{
        this->scrollArea->refreshContent();
    }
}


void MainWindow::initAlarmClock(){
    QString path = QDir::currentPath() + "/ZygTimerList.text";
    QFile file(path);

    //如果文件不存在就创建
    if(!file.exists()){
        file.open(QIODevice::Truncate | QIODevice::WriteOnly);
        file.close();
    }

    if(file.open(QIODevice::ReadOnly)){
        if(0 == file.size()){
            return;
        }
        QString line = file.readLine();
        do{
            ZygTimer * zygTimer = new ZygTimer;
            QStringList slist = line.split("\t");
            QStringList timelist = slist.at(0).split(":");
            zygTimer->setHour(stringToInt(timelist.at(0)));
            zygTimer->setMinute(stringToInt(timelist.at(1)));
            zygTimer->setSecond(stringToInt(timelist.at(2)));
            QString soundFileName = slist.at(1);
            soundFileName.replace("\r\n","");
            zygTimer->setSoundFilePath(soundFileName);
            ZygTimer::addTimer(zygTimer);
            line = file.readLine();
        }while(line != nullptr);
        file.close();
    }
}

void MainWindow::closeEvent(QCloseEvent *event){
    QApplication::setQuitOnLastWindowClosed(true);
    this->hide();
    event->ignore();
}

